package com.boco.fznq.classify;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.boco.fznq.AppConstants;
import com.boco.fznq.R;
import com.boco.fznq.network.MyHttpTransport;
import com.boco.fznq.webview.MyWebviewActivity;
import com.culiu.core.fragment.BaseCoreMVPFragment;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 项目的fragment
 * Created by jiangcheng on 2016/12/12.
 */

public class ProjectFragment extends BaseCoreMVPFragment<ProjectPresenter, IProjectUI>
        implements IProjectUI {
    /**
     * 交易公告
     */
    public static final String DEAL_NOTICE = "1";

    /**
     * 成交公告
     */
    public static final String TRADING_NOTICE = "0";

    @BindView(R.id.rl_trade_info)
    RelativeLayout mRlTradeInfo;
    @BindView(R.id.rl_exchange_info)
    RelativeLayout mRlExchangeInfo;
    @BindView(R.id.tv_test)
    TextView mTvTest;
    @BindView(R.id.tv_content)
    TextView mTvContent;

    Unbinder unbinder;
    private View mView;


    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (msg.obj != null && msg.obj instanceof String) {
                        mTvContent.setText((String) msg.obj);
                    }
                    break;
                case 0:

                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_classify, container, false);
        unbinder = ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    protected ProjectPresenter createPresenter() {
        return new ProjectPresenter();
    }

    @Override
    protected IProjectUI getUi() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_trade_info, R.id.rl_exchange_info, R.id.tv_test})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_trade_info:
                MyWebviewActivity.startMyWebView(getActivity(),
                        "http://nccq.fzztb.gov.cn/fznccqwz/jyxx/003001/003001001/");
                break;
            case R.id.rl_exchange_info:
                MyWebviewActivity.startMyWebView(getActivity(),
                        "http://nccq.fzztb.gov.cn/fznccqwz/jyxx/003001/003001003/");
                break;
            case R.id.tv_test:
//                requestTest();
                break;
        }
    }

    /**
     * soap 请求协议测试
     */
    private void requestTest() {
        // 请求的url
        String url = "http://nccq.fzztb.gov.cn/EpointWebService/YeWuInfo.asmx";

        // 命名空间
        String nameSpace = "http://nccq.fzztb.gov.cn";

        // 请求动作
        final String SOAPAction = "http://epoint.com.cn/GetJYGGOrCYGG";

        String methodName = "GetJYGGOrCYGG";

        SoapObject object = new SoapObject(nameSpace, methodName);
        object.addAttribute("StatusCode", DEAL_NOTICE);
        object.addAttribute("strWhere", "");
        object.addAttribute("VerifyCode", AppConstants.EPOINT_KEY);


        //指定SOAP协议版本1.1
        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        //是否调用.net开发的接口
        envelope.dotNet = true;
        //指定传入的参数
        envelope.bodyOut = object;

        //Http请求对象
        final MyHttpTransport httpTransportSE = new MyHttpTransport(url);
        httpTransportSE.debug = true;

        new Thread() {

            @Override
            public void run() {

                Log.d("jiangcheng", "run is execute");

                try {
                    //发送请求
                    httpTransportSE.call(SOAPAction, envelope);
                    //拿到响应体
                    SoapObject response = (SoapObject) envelope.bodyIn;
                    //取数据
                    String data = response.getProperty("GetJYGGOrCYGGResult").toString();

                    Message msg = Message.obtain();
                    msg.obj = data;
                    msg.what = 1;
                    mHandler.sendMessage(msg);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
}
